<?php
class My_xmlrpc_metaWeblog {
	public function __construct() {
		$this->db = Zend_Registry::get ( 'db' );
	}
	
	/** 
	 *@param string $blogid 
	 *@param string $username 
	 *@param string $password    
	 *@param int $numberOfPosts   
	 *@return array 
	 */
	public function getRecentPosts($p0, $username, $password, $p3) {
		$login = new My_Login ( );
		$data = $login->login ( $username, $password );
		
		$select = new Zend_Db_Select ( $this->db );
		$select->from ( 'bc_news' );
		$select->limit ( $p3, 0 )->order ( 'time DESC' );
		$select->where ( 'news_uid = ?', $data->id );
		$d = $this->db->fetchAll ( $select );
		
		foreach ( $d as $k => $v ) {
			$a [] = $this->metaFormat ( $v );
		}
		return $a;
	}
	/** 
	 *@param string $blogid 
	 *@param string $username 
	 *@param string $password    
	 *@return array 
	 */
	public function getPost($p0, $p1, $p2) {
		$d = new Article ( );
		$d = $d->find ( $p0 )->toArray ();
		$d = $d [0];
		//		$a ['dateCreated'] = '20080506T08:29:05';
		//		$a ['link'] = 'comet';
		//		$a ['permaLink'] = 'comet';
		//		$a ['categories'] = array ('comet' );
		//		$a ['mt_excerpt'] = array ('comet' );
		//		$a ['mt_text_more'] = array ('comet' );
		//		$a ['mt_allow_comments'] = array ('comet' );
		//		$a ['mt_allow_pings'] = array ('comet' );
		//		$a ['mt_keywords'] = array ('comet' );
		//		$a ['wp_slug'] = array ('comet' );
		//		$a ['wp_password'] = array ('comet' );
		//		$a ['wp_author_id'] = array ('comet' );
		//		$a ['wp_author_display_name'] = array ('comet' );
		//		$a ['date_created_gmt'] = array ('comet' );
		//		$a ['post_status'] = array ('comet' );
		//		$a ['custom_fields'] = array ('comet' );
		$a = $this->metaFormat ( $d );
		return $a;
	}
	/**
	 *@param string postid
	 *@param string username
	 *@param string password
	 *@param struct content
	 *@param boolean publish
	 */
	public function editPost($postid, $username, $password, $content, $publish) {
		$blog = new Article ( );
		$blogDate = $blog->find ( $postid )->current ();
		$blogDate->news_title = $content ['title'];
		$blogDate->news_content = $content ['description'];
		if ($content ['categories'] [0]) {
			$a = My_xmlrpc_category::getHushFlip ();
			$categorieId = $a [$content ['categories'] [0]];
			$blogDate->column_id = $categorieId;
		}
		try {
			$blogDate->save ();
		} catch ( PDOException $e ) {
			echo "Exception caught : {$e->getMessage()}\n";
		}
		return true;
	}
	
	/**
	 *@param string blogid 
	 *@param string username
	 *@param string password
	 *@param struct content
	 *@param boolean publish
	 */
	public function newPost($blogid, $username, $password, $content, $publish) {
		$login = new My_Login ( );
		$data = $login->login ( $username, $password );
		if (! $data)
			throw new Zend_XmlRpc_Server_Exception ( '403', 403 );
		$content ['userid'] = $data->id;
		
		if ($content ['categories'] [0]) {
			$a = My_xmlrpc_category::getHushFlip ();
			$categorieId = $a [$content ['categories'] [0]];
		} else {
			$kid = $data->ke_id;
			$a = My_xmlrpc_category::getGroup ( $kid );
			$categorieId = $a [0] ['id'];
		}
		$blog = new Article ( );
		$content = $this->unmetaFormat ( $content );
		$content ['column_id'] = $categorieId;
		$content ['section_id'] = $data->ke_id;
		
		try {
			$id = $blog->insert ( $content );
		} catch ( PDOException $e ) {
			echo "Exception caught : {$e->getMessage()}\n";
		}
		return $id;
	}
	
	protected function metaFormat($arr0) {
		$arr ['title'] = $arr0 ['news_title'];
		$arr ['postid'] = $arr0 ['id'];
		$arr ['userid'] = $arr0 ['news_uid'];
		$arr ['description'] = $arr0 ['news_content'];
		return $arr;
	}
	protected function unmetaFormat($arr0) {
		$arr ['news_title'] = $arr0 ['title'];
		$arr ['news_uid'] = $arr0 ['userid'];
		$arr ['news_content'] = $arr0 ['description'];
		$arr ['time'] = time ();
		$arr ['mode'] = 0;
		$arr ['news_view'] = 0;
		return $arr;
	}
	/**
	 *@param string $blog_id 
	 *@param string $username 
	 *@param string $password    
	 *@param struct $data    
	 */
	public function newMediaObject($blog_id, $username, $password, $data) {
		$a = new Pic ( );
		$d ['summary'] = $data ['name'];
		$d ['pic_type'] = $data ['type'];
		$d ['pic_bits'] = $data ['bits'];
		
		try {
			$id = $a->insert ( $d );
		} catch ( PDOException $e ) {
			"Exception caught : {$e->getMessage()}\n";
		}
		
		return array ('url' => 'index.php/pic/view/s/400/id/' . $id );
	}
	/**
	 *@param string $appkey
	 *@param string $postid
	 *@param string $username
	 *@param string $password
	 *@param string $password
	 */
	public function getTemplate($appkey, $blogid, $username, $password, $templateType) {
		$filename = '1.txt';
		
		$f = fopen ( $filename, 'r' );
		$content = fread ( $f, filesize ( $filename ) );
		fclose ( $f );
		return $content;
	}
}

?>